package com.pengcheng.taskapplication;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDexApplication;
import android.util.Log;
import android.widget.Toast;

import com.image.imagepicker.ImagePicker;
import com.pengcheng.taskapplication.util.AbContextUtils;
import com.pengcheng.taskapplication.util.ImagePickerLoader;
import com.pgyersdk.Pgyer;
import com.pgyersdk.PgyerActivityManager;
import com.pgyersdk.crash.PgyCrashManager;
import com.pgyersdk.crash.PgyerCrashObservable;
import com.pgyersdk.crash.PgyerObserver;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.lang.reflect.Field;
import java.util.Stack;

/**
 * Created by ZFT on 2020/8/4.
 */

public class TaskApplication extends MultiDexApplication {
    private static TaskApplication app;

    private static Stack<Activity> activityStack;
    private static Stack<Activity> activityMainStack;

    public static TaskApplication getInstance() {
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        AbContextUtils.init(this);
        initImagePicker();
        initPayger();
        //集成扫描二维码
        ZXingLibrary.initDisplayOpinion(this);
    }
    public void initPayger(){
        PgyCrashManager.register(getApplicationContext());  // 弃用
        PgyCrashManager.register();
        PgyerCrashObservable.get().attach(new PgyerObserver() {
            @Override
            public void receivedCrash(Thread thread, Throwable throwable) {

            }
        });
        PgyerActivityManager.set(this);
    }

    private void initImagePicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new ImagePickerLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        //imagePicker.setCrop(true);                            //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);                   //是否按矩形区域保存
        imagePicker.setSelectLimit(9);                        //选中数量限制
        //imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        //imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        //imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素
    }

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        Pgyer.setAppId("b286a1423abbf81df3dcad90b7f1d9e9");
    }


    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null)
            activityStack = new Stack<Activity>();
        activityStack.add(activity);
    }
    /**
     * 添加Activity到堆栈
     */
    public void addMainActivity(Activity activity) {
        if (activityMainStack == null)
            activityMainStack = new Stack<Activity>();
        activityMainStack.add(activity);
    }
    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        if (activity != null) {
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack)
            if (activity.getClass().equals(cls))
                finishActivity(activity);
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (activityStack!=null&&activityStack.size()>0){
            for (int i = 0, size = activityStack.size(); i < size; i++)
                if (null != activityStack.get(i))
                    activityStack.get(i).finish();
        }
        activityStack.clear();
    }

    /**
     * 结束掉主Activity
     */
    public void finishMainActivity() {
        for (int i = 0, size = activityMainStack.size(); i < size; i++)
            if (null != activityMainStack.get(i))
                activityMainStack.get(i).finish();
        activityMainStack.clear();
    }


    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
        }
    }

    /**
     * 退出程序
     */
    private long exitTime = 0;
    public void exit(Context context) {
        if((System.currentTimeMillis()-exitTime) > 2000){
            Toast.makeText(context, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            TaskApplication.getInstance().AppExit(context);
        }
    }
}
